﻿#include <iostream>
#include <string>
#include <exception>
#include <thread>
using namespace std;
int Add(int a, int b)
{
	/*try
	{*/
		if (b > 0 && a > 0)
		{
			string s("the number is not more than zero");
			//cout << __FUNCTION__ << ":" << __LINE__ << "行执行" << endl;
			throw s;
		}
		/*else
		{
			return a + b;
		}*/
	//}
	/*catch(const string& errid)
	{
		cout << errid << endl;
	}
	catch (const char* x)
	{
		cout << x << endl;

	}*/
	return a + b;
}
void func()
{
	int* array = new int[10];
	int* array2 = new int[10];
	try
	{
		int a, b;
		cin >> a >> b;
		cout << Add(a, b) << endl;
	}
	catch (...)
	{
		cout << "delete[]" << array << endl;
		cout << "delete[]" << array2 << endl;

		throw;
	}
	//cout << __FUNCTION__ << ":" << __LINE__ << "行执行" << endl;
	// 当出现异常时，异常直接被Add函数中的catch捕获，不会执行这里的异常捕获，
	// new出来的空间没有释放造成了内存泄漏，所以要在这里手动释放
	cout << "delete[]" << array << endl;
	cout << "delete[]" << array2 << endl;

	delete[]array;
}

class Exception
{
public:
	Exception(const string& errmsg, int id)
		:_errmsg(errmsg)
		, _id(id)
	{}
	virtual string what() const
	{
		return _errmsg;
	}
	int getid() const
	{
		return _id;
	}
protected:
	string _errmsg;
	int _id;
};

class SqlException : public Exception
{
public:
	SqlException(const string& errmsg, int id, const string& sql)
		:Exception(errmsg, id)
		, _sql(sql)
	{}
	virtual string what() const
	{
		string str = "SqlException:";
		str += _errmsg;
		str += "->";
		str += _sql;
		return str;
	}
private:
	const string _sql;
};
class CacheException : public Exception
{
	public:
	CacheException(const string& errmsg, int id)
	:Exception(errmsg, id)
	{}
	virtual string what() const
	{
	string str = "CacheException:";
	str += _errmsg;
	return str;
	}
};
class HttpException : public Exception
{
public:
	HttpException(const string& errmsg, int id, const string& type)
		:Exception(errmsg, id)
		, _type(type)
	{}
	virtual string what() const
	{
		string str = "HttpException:";
		str += _type;
		str += ":";
		str += _errmsg;
		return str;
	}
private:
	const string _type;
};

void SQLMgr()
{
	if (rand() % 7 == 0)
	{
		throw SqlException("权限不⾜", 100, "select * from name = '张三'");
	}
	else
	{
		cout << "SQLMgr 调⽤成功" << endl;
	}
}
void CacheMgr()
{
	if (rand() % 5 == 0)
	{
		throw CacheException("权限不足", 100);
	}
	else if (rand() % 6 == 0)
	{
		throw CacheException("数据不存在", 101);
	}
	else
	{
		cout << "CacheMgr 调⽤成功" << endl;
	}
	SQLMgr();
}
void HttpServer()
{
	if (rand() % 3 == 0)
	{
		throw HttpException("请求资源不存在", 100, "get");
	}
	else if (rand() % 4 == 0)
	{
		throw HttpException("权限不⾜", 101, "post");
	}
	else
	{
		cout << "HttpServer调⽤成功" << endl;
	}
	CacheMgr();
}
//int main()
//{
//	/*while (1)
//	{
//		this_thread::sleep_for(chrono::seconds(1));
//		try
//		{
//			func();
//		}
//		catch (const string& errmsg)
//		{
//			cout << errmsg << endl;
//		}
//	}*/
//	srand(time(0));
//	while (1)
//	{
//		this_thread::sleep_for(chrono::seconds(1));
//		try
//		{
//			HttpServer();
//		}
//		catch (const Exception& e) // 这⾥捕获基类，基类对象和派⽣类对象都可以被捕获
//		{
//		cout << e.what() << endl;
//		}
//			catch (...)
//		{
//			cout << "Unkown Exception" << endl;
//		}
//	}
//	return 0;
//}


void _sendmsg(const string& msg)
{
	if (rand() % 2 == 0)
	{
		throw HttpException("网络不稳定，发送失败", 102, "put");
	}
	else if (rand() % 7 == 0)
	{
		throw HttpException("你不是该对象的好友", 103, "put");
	}
	else
	{
		cout << "发送成功" << endl;
	}
}
void sendmsg(const string& msg)
{
	for (int i = 0; i < 4; i++)
	{
		try
		{
			_sendmsg(msg);
			break;
		}
		catch (const Exception& e)
		{
			if (e.getid() == 102)
			{
				if (i == 3)
					throw;
				cout << "开始第" << i + 1 << "次重试" << endl;
			}
			else throw;
		}
	}
}
//int main()
//{
//	string str;
//	while (cin >> str)
//	{
//		try
//		{
//			sendmsg(str);
//		}
//		catch (const Exception& s)
//		{
//			cout << s.what() << endl;
//		}
//		catch (...)
//		{
//			cout << "unkown Exception" << endl;
//		}
//	}
//	return 0;
//}

int main()
{
	while (1)
	{


		try
		{
			func();
		}
		catch (const char* errmsg)
		{
			cout << errmsg << endl;
		}
		catch (const exception& e)
		{
			cout << e.what() << endl;
		}
		catch (...)
		{
			cout << "unkown exception" << endl;
		}
	}
	return 0;
}